package com.example.triprec.ui.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.example.triprec.R;
import com.example.triprec.common.Result;
import com.example.triprec.dao.CommentDao;
import com.example.triprec.entity.Comment;
import com.example.triprec.entity.Trip;
import com.example.triprec.utils.CurrentUserUtils;

/**
 * 行程评价对话框
 */
public class CommentDialog {

    private Context context;
    private Trip trip;
    private Comment comment;
    private boolean isViewMode; // 查看模式，不可编辑

    private AlertDialog dialog;
    private OnCommentListener mListener;

    public interface OnCommentListener {
        void onCommentSuccess(Comment comment);
    }

    /**
     * 添加评价构造方法
     */
    public CommentDialog(Context context, Trip trip) {
        this.context = context;
        this.trip = trip;
        this.isViewMode = false;
    }

    /**
     * 查看评价构造方法
     */
    public CommentDialog(Context context, Trip trip, Comment comment) {
        this.context = context;
        this.trip = trip;
        this.comment = comment;
        this.isViewMode = true;
    }

    public void setOnCommentListener(OnCommentListener listener) {
        this.mListener = listener;
    }

    /**
     * 显示对话框
     */
    public void show() {
        if (trip == null || trip.getId() == null || trip.getScenicId() == null) {
            Toast.makeText(context, "行程信息不完整", Toast.LENGTH_SHORT).show();
            return;
        }

        // 创建对话框视图
        View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_comment, null);

        // 初始化控件
        TextView tvTitle = dialogView.findViewById(R.id.tv_title);
        EditText etComment = dialogView.findViewById(R.id.et_comment);
        RatingBar ratingBar = dialogView.findViewById(R.id.rating_bar);

        // 设置标题
        String title = isViewMode ? "评价详情" : "添加评价";
        tvTitle.setText(title);

        // 如果是查看模式，填充评价内容并禁用编辑
        if (isViewMode && comment != null) {
            etComment.setText(comment.getContent());
            if (comment.getScore() != null) {
                ratingBar.setRating(comment.getScore().floatValue());
            }
            etComment.setEnabled(false);
            ratingBar.setIsIndicator(true);
        }

        // 创建对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setView(dialogView);

        if (isViewMode) {
            builder.setNegativeButton("关闭", null);
        } else {
            builder.setPositiveButton("确定", null);
            builder.setNegativeButton("取消", null);
        }

        dialog = builder.create();

        dialog.show();

        // 设置提交按钮点击事件
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isViewMode) {
                    dialog.dismiss();
                } else {
                    submitComment(etComment.getText().toString(), ratingBar.getRating());
                }
            }
        });
    }

    /**
     * 提交评价
     */
    private void submitComment(String content, float rating) {
        if (content.trim().isEmpty()) {
            Toast.makeText(context, "请输入评价内容", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            // 获取当前登录用户
            long userId = CurrentUserUtils.getCurrentUser().getId();

            // 创建评价对象
            Comment newComment = new Comment();
            newComment.setContent(content);
            newComment.setScore((double) rating);
            newComment.setUserId(userId);
            newComment.setScenicId(trip.getScenicId());
            newComment.setTripId(trip.getId());

            // 调用DAO添加评价
            Result<Void> result = CommentDao.addComment(newComment);

            if (result.isSuccess()) {
                // 添加成功后，查询回填Comment对象以获取ID和创建时间
                Result<Comment> queryResult = CommentDao.getByTripId(trip.getId());

                // 回调
                if (queryResult.isSuccess() && mListener != null) {
                    mListener.onCommentSuccess(queryResult.getData());
                }

                Toast.makeText(context, "评价成功", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            } else {
                Toast.makeText(context, result.getMessage(), Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            Toast.makeText(context, "评价失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
} 